package com.weasel.weixin.mp;

import com.weasel.helper.ClassPathFileReader;
import com.weasel.weixin.commons.exception.UnSupportFileException;
import com.weasel.weixin.commons.media.MediaResponse;
import com.weasel.weixin.commons.media.bean.Image;
import com.weasel.weixin.commons.media.bean.Media;
import com.weasel.weixin.commons.media.bean.Video;
import com.weasel.weixin.commons.media.bean.Voice;
import com.weasel.weixin.mp.builder.MpConfigBuilder;
import com.weasel.weixin.mp.config.MpConfig;
import com.weasel.weixin.mp.request.MpTemporaryMediaRequest;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ExecutionException;

/**临时素材测试
 * 运行main方法，通过注释和打开注释来测试各方法
 * Created by Dylan on 2015/9/29.
 */
public class MpTemporaryMediaRequestDemo {

    public static void main(String [] args) throws IOException, ExecutionException {

        MpConfig config = MpConfigBuilder.fromXML("classpath:weixin-config.xml");

        MpTemporaryMediaRequest request = new MpTemporaryMediaRequest(config);

        image(request);

     //   video(request);

       // voice(request);

    }

    private static void image(MpTemporaryMediaRequest request) throws IOException, ExecutionException {
        InputStream fileInputStream = ClassPathFileReader.readToInputStrream("classpath:mm.jpeg");
        File file = new File("media.jpeg");
        FileUtils.copyInputStreamToFile(fileInputStream,file);
        Media image = new Image();
        image.setFile(file);
        MediaResponse response = request.upload(image);
        if(!response.isOK()){
            throw new UnSupportFileException(response.getErrmsg());
        }
        request.download(response.getMediaId(), ClassPathFileReader.getClassPath());
    }

    private static void video(MpTemporaryMediaRequest request) throws IOException, ExecutionException {
        InputStream fileInputStream = ClassPathFileReader.readToInputStrream("classpath:mm.mp4");
        File file = new File("media.mp4");
        FileUtils.copyInputStreamToFile(fileInputStream,file);
        Media video = new Video();
        video.setFile(file);
        MediaResponse response = request.upload(video);

        if(!response.isOK()){
            throw new UnSupportFileException(response.getErrmsg());
        }
        request.download(response.getMediaId(),ClassPathFileReader.getClassPath());
    }

    private static void voice(MpTemporaryMediaRequest request) throws IOException, ExecutionException {
        InputStream fileInputStream = ClassPathFileReader.readToInputStrream("classpath:123.mp3");
        File file = new File("media.mp3");
        FileUtils.copyInputStreamToFile(fileInputStream,file);
        Media voice = new Voice();
        voice.setFile(file);
        MediaResponse response = request.upload(voice);
        if(!response.isOK()){
            throw new UnSupportFileException(response.getErrmsg());
        }
        response = request.download(response.getMediaId(),ClassPathFileReader.getClassPath());
    }
}
